package com.pl.lc.user.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pl.lc.common.config.SystemConfig;
import com.pl.lc.common.util.JwtUtil;
import com.pl.lc.common.vo.R;
import com.pl.lc.user.entity.MoneyLog;
import com.pl.lc.user.entity.PayLog;
import com.pl.lc.user.mapper.MoneyLogMapper;
import com.pl.lc.user.mapper.PayLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * 7.用户钱包变动表 前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-11
 */
@RestController
@RequestMapping("/user")
public class MoneyLogController {
    @Autowired
    private MoneyLogMapper moneyLogMapper;
    @Autowired
    private PayLogMapper payLogMapper;
    @RequestMapping("/moneyLog")
    public R consume(HttpServletRequest request) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<MoneyLog>moneyLogQueryWrapper=new QueryWrapper<>();
        moneyLogQueryWrapper.eq("user_id",id);
        List<MoneyLog> moneyLogs = moneyLogMapper.selectList(moneyLogQueryWrapper);
        if (moneyLogs.size()>0){
            return R.ok(moneyLogs);
        }
        return R.fail("没有消费数据");
    }

    @RequestMapping("/payLog")
    public R payLog(HttpServletRequest request) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<PayLog>payLogQueryWrapper=new QueryWrapper<>();
        payLogQueryWrapper.eq("user_id",id);
        List<PayLog> payLogs = payLogMapper.selectList(payLogQueryWrapper);
        if (payLogs.size()>0){
            return R.ok(payLogs);
        }
        return R.fail("没有充值数据");
}
}

